﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Diagnostics;

namespace ResourceEditor.Core
{
    internal static class ILHelper
    {
        #region Disassemble
        private const string ILDASM = "ildasm.exe";

        internal static void Disassemble(FileInfo workingFile)
        {
            try
            {
                Process process = Process.Start(IldasmPath, IldasmArguments(workingFile));
                process.WaitForExit();
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
                throw;
            }
        }

        private static string IldasmArguments(FileInfo workingFile)
        {
            return string.Format("/out=\"{0}.il\" \"{0}{1}\"", workingFile.FullNameWithoutExtension(), workingFile.Extension);
        }

        private static string IldasmPath
        {
            get
            {
                string ildasmPath = ConfigurationManager.AppSettings["ildasmPath"];
                if (string.IsNullOrEmpty(ildasmPath) || !File.Exists(ildasmPath))
                {
                    ildasmPath = IOExtensions.Combine(AppDomain.CurrentDomain.BaseDirectory, "ILTools", ILDASM);
                }

                if (!File.Exists(ildasmPath))
                {
                    Logger.Error(Messages.IldasmNotFound);
                    throw new FileNotFoundException(Messages.IldasmNotFound, ILDASM);
                }

                return ildasmPath;
            }
        }
        #endregion

        #region Reassemble
        private const string ILASM = "ilasm.exe";

        internal static void Reassemble(FileInfo workingFile)
        {
            try
            {
                Process process = Process.Start(IlasmPath, IlasmArguments(workingFile));
                process.WaitForExit();
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
                throw;
            }
        }

        private static string IlasmArguments(FileInfo workingFile)
        {
            return string.Format("\"{0}.il\" /{1} /resource=\"{0}.res\"", workingFile.FullNameWithoutExtension(), workingFile.Extension.Substring(1));
        }

        private static string IlasmPath
        {
            get
            {
                string frameworkRootDir = Path.GetDirectoryName(typeof(int).Module.FullyQualifiedName);
                string ilasmPath = Path.Combine(frameworkRootDir, ILASM);

                if (!File.Exists(ilasmPath))
                {
                    ilasmPath = ConfigurationManager.AppSettings["ilasmPath"];
                    if (string.IsNullOrEmpty(ilasmPath) || !File.Exists(ilasmPath))
                    {
                        ilasmPath = IOExtensions.Combine(AppDomain.CurrentDomain.BaseDirectory, "ILTools", ILASM);
                    }
                }

                if (!File.Exists(ilasmPath))
                {
                    Logger.Error(Messages.IlasmNotFound);
                    throw new FileNotFoundException(Messages.IlasmNotFound, ILASM);
                }

                return ilasmPath;
            }
        }
        #endregion
    }
}
